<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">房间类型名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入房间了类型名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="roomTypeBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
                            <button id="roomTypeBtnExport" class="layui-btn icon-btn color-green">导出</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="roomTypeTable" lay-filter="roomTypeTable"></table>
        </div>
    </div>
</div>
<style>
    form[lay-filter="roomTypeForm"] label{
        width: 100px;
    }
    form[lay-filter="roomTypeForm"] .layui-form-item  .layui-input-inline{
        width: calc(100% - 180px);
    }
</style>
<!-- 表格操作列 -->
<script type="text/html" id="roomTypeTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="roomTypeForm">
    <form lay-filter="roomTypeForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <div class="layui-form-item">
            <label class="layui-form-label">空间性质</label>
            <div class="layui-input-inline">
                <select name="spaceCharacterId" id="spaceCharacterId" lay-filter="spaceCharacterId" required
                        lay-verify="required" placeholder="请选择空间性质">
                    <option value=""> 请选择</option>
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux">
                <a href="#/system/spaceCharacter" class="layui-btn layui-btn-xs" ew-event="closeDialog" title="添加空间性质">
                    <i class="layui-icon layui-icon-addition"></i>
                </a>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">房间类型编码</label>
            <div class="layui-input-inline">
                <input name="code" placeholder="请输入房间类型编码" type="text" class="layui-input" lay-verify="required"
                       required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">房间类型名称</label>
            <div class="layui-input-inline">
                <input name="name" placeholder="请输入房间类型名称" type="text" class="layui-input" lay-verify="required"
                       required/>
            </div>
        </div>
        <div class="layui-form-item" style="display: none" id="peopleNumber">
            <label class="layui-form-label">可住人数</label>
            <div class="layui-input-inline">
                <input name="peopleNumber" placeholder="请输入可住人数" type="text" class="layui-input"
                       lay-verify="required"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-inline">
                <textarea id="remark" name="remark" placeholder="请输入描述" class="layui-textarea"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-inline">
                <input name="deleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">序号</label>
            <div class="layui-input-inline">
                <input name="sortNumber" placeholder="请输入序号" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <label class="layui-form-label"></label>
            <div class="layui-input-inline">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                <button class="layui-btn" lay-filter="roomTypeFormSubmit" lay-submit>保存</button>
            </div>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#roomTypeTable',
            url: config.base + 'enterprise/system/admin/roomType/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'sortNumber', sort: true, title: '序号', fixed: 'left'}
                , {field: 'code', sort: true, title: '房间类型编码', fixed: 'left'}
                , {field: 'name', sort: true, title: '房间类型名称'}
                , {field: 'peopleNumber', sort: true, title: '可住人数'}
                , {field: 'spaceCharacterName', sort: true, title: '空间性质'}
                , {
                    field: 'deleted', sort: true, title: '是否启用', templet: function (d) {
                        if (d.deleted) {
                            return '<input name="deleted" type="checkbox" lay-skin="switch" value="' + d.id + '" checked="' + d.deleted + '" lay-filter="deleted"  lay-text="启用|停用"/>';
                        } else {
                            return '<input name="deleted" type="checkbox" lay-skin="switch" value="' + d.id + '" lay-filter="deleted"   lay-text="启用|停用"/>';
                        }
                    }
                }
                , {field: 'createTime', sort: true, title: '创建时间'}
                , {align: 'center', toolbar: '#roomTypeTableBar', title: '操作', width: 200, fixed: 'right'}
            ]],
        });

        // 工具条点击事件
        table.on('tool(roomTypeTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(roomTypeTable)', function (obj) {
            table.reload('roomTypeTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', data => {
            insTb.reload({where: admin.resetSearch(data.field), page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#roomTypeBtnAdd').click(function () {
            showEditModel();
        });

        form.on('switch(deleted)', function (obj) {
            if (obj.elem.checked) {
                enable(obj.value)
            } else {
                disable(obj.value)
            }
        })

        function enable(id) {
            admin.req('enterprise/system/admin/roomType/enable', {id: id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    insTb.reload('systemWorkmanshipTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        function disable(id) {
            admin.req('enterprise/system/admin/roomType/disable', {id: id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    insTb.reload('systemWorkmanshipTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '700px',
                offset: '65px',
                title: data ? '修改房间类型' : '添加房间类型',
                content: $('#roomTypeForm').html(),
                success: function () {
                    if (data) {
                        isShowPeople(data.spaceCharacterName)
                    } else {
                        isShowPeople()
                    }
                    admin.reqSync('enterprise/system/admin/spaceCharacter/list', {limit: 9999}, function (res) {
                        if (res.code == 200) {
                            loadReportSelectHtml(res.data, '请选择', 'spaceCharacterId', 1, 'roomTypeForm')
                        } else {
                            loadReportSelectHtml([], '请选择', 'spaceCharacterId', 1, 'roomTypeForm')
                        }
                    }, 'GET');
                    //监听空间性质
                    form.on('select(spaceCharacterId)', function (data) {
                        isShowPeople(data.elem[data.elem.selectedIndex].text);
                    });

                    //是否显示 可以住人数
                    function isShowPeople(_data) {
                        if (_data == "宿舍") {
                            $("#peopleNumber").show();
                            $("#peopleNumber").find('input').attr('lay-verify', 'required');
                            $("#peopleNumber").find('input').val('')
                        } else {
                            $("#peopleNumber").hide();
                            $("#peopleNumber").find('input').attr('lay-verify', '');
                            $("#peopleNumber").find('input').val(0)
                        }
                    }


                    form.val('roomTypeForm', data);
                    form.render('select');

                    // 表单提交事件
                    form.on('submit(roomTypeFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprise/system/admin/roomType/update' : 'enterprise/system/admin/roomType/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('roomTypeTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/system/admin/roomType/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#roomTypeBtnExport').click(function () {
            var checkRows = table.checkStatus('roomTypeTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

        // 渲染下拉框
        function loadReportSelectHtml(data, tips, htmltarget, type, parentForm) {
            let html = ' <option value="">' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type == 1) {
                    html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                } else if (type == 2) {
                    html += `<option value='${data[i].cabCode}'>${data[i].cabCode}</option>`;
                }
            }
            $("form[lay-filter='" + parentForm + "']").find("select[name='" + htmltarget + "']").html(html);
            form.render('select');
        }

    });
</script>